/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.resrev.web.page;

import com.rafkind.resrev.dao.UserDao;
import com.rafkind.resrev.data.User;
import com.rafkind.resrev.web.Application;
import com.rafkind.resrev.web.Session;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.FormComponentFeedbackBorder;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author dave
 */
public class LoginPage extends WebPage { 
  
  public LoginPage() {    
    LoginForm lf = new LoginForm("loginForm");    
    add(lf);   
    
    add(new Label("title", new PropertyModel(getApplication(), "title")));
    add(new Label("version", new PropertyModel(getApplication(), "version")));
    add(new Label("licencee", new PropertyModel(getApplication(), "licencee")));

    add(new ExternalLink("adminEmailLink", 
        new Model("mailto:" + ((Application)getApplication()).getAdminEmail()),
        new PropertyModel(getApplication(), "adminEmail")));
    
  }
  
  public static class LoginForm extends StatelessForm {
    
    @SpringBean 
    private UserDao userDao;
    
    private String loginName;
    private String password;
    
    public LoginForm(String id) {
      super(id);      
      
      
      add(new FeedbackPanel("feedback"));
      
      add(new TextField("userNameField", 
          new PropertyModel(this, "loginName")));
      add(new FormComponentFeedbackBorder("passwordFieldBorder")
          .add(new PasswordTextField("passwordField", 
            new PropertyModel(this, "password")).setLabel(new Model("Password"))));
      add(new Button("loginButton"));
    }   
    
    @Override
    protected void onSubmit() {
      //System.out.println("888\n888 Loggin in as " + loginName + "/" + password + "\n888");      
      User user = userDao.authenticateUser(loginName, password);
      if (user != null) {
        ((Session)getSession()).setCurrentUser(user);
        if (!continueToOriginalDestination()) {
          setResponsePage(getApplication().getHomePage());
        } else {
          System.out.println("Continueing on to original destination..");
        }
      } else {
        error("Invalid Username/Password");
      }
    }    

    public String getLoginName() {
      return loginName;
    }

    public void setLoginName(String loginName) {
      this.loginName = loginName;
    }

    public String getPassword() {
      return password;
    }

    public void setPassword(String password) {
      this.password = password;
    }
  }
}